package io.renren.modules.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.pet.dao.PetSendCommentDao;
import io.renren.modules.pet.vo.PetSendCommentVO;
import io.renren.modules.pet.dto.PetSendCommentDTO;
import io.renren.modules.pet.entity.PetSendCommentEntity;
import io.renren.modules.pet.service.PetSendCommentService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 送养宠物评论表
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-21
 */
@Service
public class PetSendCommentServiceImpl extends CrudServiceImpl<PetSendCommentDao, PetSendCommentEntity, PetSendCommentVO> implements PetSendCommentService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<PetSendCommentEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PetSendCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void save(PetSendCommentDTO petSendCommentDTO) {
        PetSendCommentEntity petSendCommentEntity = ConvertUtils.sourceToTarget(petSendCommentDTO, PetSendCommentEntity.class);
        this.insert(petSendCommentEntity);
    }

    @Override
    public void update(PetSendCommentDTO petSendCommentDTO) {
        PetSendCommentEntity petSendCommentEntity = ConvertUtils.sourceToTarget(petSendCommentDTO, PetSendCommentEntity.class);
        this.updateById(petSendCommentEntity);
    }

    @Override
    public List<PetSendCommentVO> getPetSendComment(Long petSendId) {
        QueryWrapper<PetSendCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_send_id", petSendId);
        wrapper.orderByDesc("create_date");
        List<PetSendCommentEntity> entityList = this.baseDao.selectList(wrapper);
        List<PetSendCommentVO> voList = ConvertUtils.sourceToTarget(entityList, PetSendCommentVO.class);
        voList.forEach(e -> {
            SysUserDTO userDto = sysUserService.get(e.getCreator());
            e.setCreatorName(userDto == null ? "" : userDto.getRealName());
        });
        return voList;
    }

}